#!/data/data/com.termux/files/usr/bin/bash
folder=debian-arm64
echo "正在请求存储权限..."
termux-setup-storage
# 创建符号链接
echo "正在设置常用共享目录的符号链接..."
ln -s /data/data/com.termux/files/home/storage/shared/Download ~/Download
ln -s /data/data/com.termux/files/home/storage/shared/Documents ~/Documents
ln -s /data/data/com.termux/files/home/storage/shared/Pictures ~/Pictures
ln -s /data/data/com.termux/files/home/storage/shared/Music ~/Music
ln -s /data/data/com.termux/files/home/storage/shared/Movies ~/Movies
ln -s /data/data/com.termux/files/home/storage/sdcard ~/sdcard
echo "常用共享目录设置完成！"
echo "正在更换加速源"
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list && apt update && apt upgrade
if [ -d "$folder" ]; then
	first=1
fi
tarball="rootfs.tar.xz"
if [ "$first" != 1 ];then
	if [ ! -f $tarball ]; then
		echo "科技猫正在准备镜像…"
		wget "https://dlink.host/1drv/aHR0cHM6Ly8xZHJ2Lm1zL3UvYy8yMzkxQzZCN0JGRDFGRUU1L0VYNzA5X0VObXZwSWhZU04wRm1BSHdvQk84WGdFRG1jQUlKSVpCZ2dNc1c2aEE.xz" -O $tarball
	fi
	cur=`pwd`
	mkdir -p "$folder"
	cd "$folder"
	echo "科技猫正在解压rootfs…"
	proot --link2symlink tar -xJf ${cur}/${tarball}||:
	cd "$cur"
fi
bin=start-debian.sh
echo "writing launch script"
cat > $bin <<- EOM
#!/bin/bash
cd \$(dirname \$0)
pulseaudio --start
## For rooted user: pulseaudio --start --system
## unset LD_PRELOAD in case termux-exec is installed
unset LD_PRELOAD
command="proot"
command+=" --link2symlink"
command+=" -0"
command+=" -r $folder"
command+=" -b /dev"
command+=" -b /proc"
command+=" -b debian-arm64/root:/dev/shm"
## uncomment the following line to have access to the home directory of termux
#command+=" -b /data/data/com.termux/files/home:/root"
## uncomment the following line to mount /sdcard directly to / 
#command+=" -b /sdcard"
command+=" -w /root"
command+=" /usr/bin/env -i"
command+=" HOME=/root"
command+=" PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/local/games"
command+=" TERM=\$TERM"
command+=" LANG=zh_CN.UTF-8"
command+=" /bin/bash --login"
com="\$@"
if [ -z "\$1" ];then
    exec \$command
else
    \$command -c "\$com"
fi
EOM

if grep -q "anonymous" ~/../usr/etc/pulse/default.pa;then
    echo "module already present"
else
    echo "load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" >> ~/../usr/etc/pulse/default.pa
fi

echo "exit-idle-time = -1" >> ~/../usr/etc/pulse/daemon.conf
echo "autospawn = no" >> ~/../usr/etc/pulse/client.conf
echo "export PULSE_SERVER=127.0.0.1" >> debian-arm64/etc/profile

echo "fixing shebang of $bin"
termux-fix-shebang $bin
echo "正在制作可执行的$bin"
chmod +x $bin
echo "正在移除镜像…"
rm $tarball
# Proot DNS 修复脚本 for Termux
# 用法: ./fix_proot_dns.sh [容器路径]

# 设置颜色
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 检查容器是否存在
if [ ! -d "./debian-arm64" ]; then
    echo -e "${RED}错误: 容器路径 ./debian-arm64 不存在${NC}"
    exit 1
fi

echo -e "${GREEN}开始修复 Proot 容器 DNS 问题...${NC}"

rm -rf ./debian-arm64/etc/resolv.conf
cat << EOF > "./debian-arm64/etc/resolv.conf"
nameserver 1.1.1.1
nameserver 1.0.0.1
nameserver 8.8.8.8
nameserver 8.8.4.4
options edns0 single-request-reopen
EOF

chmod 644 "./debian-arm64/etc/resolv.conf"

echo -e "${GREEN}测试结果:${NC}"
proot -r "./debian-arm64" -b /etc/resolv.conf curl -Is https://google.com | head -n 1 || echo -e "${RED}DNS 测试失败${NC}"

echo -e "${GREEN}修复完成!${NC}"

if [ "$first" == 1 ]; then
	echo "已安装系统，为您自动启动"
	rm ./debian-arm64.sh
	./start-debian.sh
fi
echo "你可以使用 ./${bin} 命令启动它了。"
rm ./debian-arm64.sh